﻿using IDSS.Core.Interfaces;
using System;
using System.Reflection;

namespace IDSS.Core
{
    /// <summary>
    /// 工厂类
    /// </summary>
    public static class Factory
    {
        /// <summary>
        /// 获取实例
        /// </summary>
        /// <typeparam name="T"><see cref="IIDBase"/> 类型</typeparam>
        /// <returns><see cref="IIDBase"/> 实体</returns>
        public static T GetEntity<T>() 
            where T : IIDBase
        {
            Type t_type = typeof(T);
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assemly in assemblies)
            {
                foreach (var type in assemly.GetTypes())
                {
                    if (!type.IsInterface && !type.IsAbstract && t_type.IsAssignableFrom(type))
                    {
                        return (T)Activator.CreateInstance(type);
                    }
                }
            }

            return default(T);
        }
    }
}
